package processingdata.density;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class DensityMerger {

	public static void main(String[] args) throws IOException {
		String dirName = "project3cs424/density/final_split";
		ArrayList<String> files = new ArrayList<String>();
		getFilesInDirectory(dirName, files);

		BufferedWriter outputWriter = new BufferedWriter(new FileWriter(
				new File("data/density.data")));
		for (int i = 0; i < files.size(); i++) {
			File file = new File(files.get(i));
			BufferedReader inputReader = new BufferedReader(
					new FileReader(file));

			while (inputReader.ready()) {
				String inputLine = inputReader.readLine();
				if (!inputLine.isEmpty()) {
					outputWriter.write(inputLine + "\n");
				}

			}

		}
		outputWriter.close();
	}

	public static void getFilesInDirectory(String dirName,
			ArrayList<String> files) {
		File dir = new File(dirName);

		// System.out.println(dir);
		if (dir.isDirectory()) {
			if (dir.getName().contains(".svn")) {
				// do nothing
			} else {

				File[] fileArray = dir.listFiles();

				for (int i = 0; i < fileArray.length; i++) {
					// System.out.println(fileArray[i]);
					getFilesInDirectory(fileArray[i].getAbsolutePath(), files);
				}

			}
		} else if (dir.isFile()) {
			files.add(dir.getAbsolutePath());
		}

	}

}
